import 'dart:convert';

import 'package:base_lib/common/base/base_theme_state.dart';
import 'package:base_lib/common/index.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/routers.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_login_lib/cache/userinfo_cache.dart';
import 'package:doctor_login_lib/login/bloc.dart';
import 'package:doctor_login_lib/repository/http.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:doctor_login_lib/login/entity/doctor_login_entity.dart'
    as LoginBean;

class QrCardPage extends StatefulWidget {
  final Parameters parameters;

  QrCardPage(this.parameters);

  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends BaseThemeState<QrCardPage> {
  var tsGrey = TextStyle(fontSize: 14, color: Colors.grey);
  var tsBlack = TextStyle(fontSize: 14, color: Colors.black);

  LoginBean.DoctorUser doctorUser;

  String base64Str;

  @override
  void initState() {
    super.initState();
    UserInfoCache.getInstance().get().then((value) {
      doctorUser = value;
      FLog(doctorUser.toJson());
      getQrImg();
    });
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      backgroundColor: theme.themeColor.themeBgColor,
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text('二维码名片'),
      ),
      body: Center(
        child: base64Str == null
            ? buildLoadingIndicator()
            : Container(
                height: ScreenAdapter.screenWidthDp,
                margin: EdgeInsets.all(15),
                padding: EdgeInsets.all(20),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(5)),
                child: Column(
                  children: [
                    Row(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        ClipRRect(
                          child: FadeInImage.assetNetwork(
                            placeholder: 'images/doctor.png',
                            image: doctorUser?.faceImage ?? '',
                            height: 60,
                            width: 60,
                          ),
                          borderRadius: BorderRadius.circular(30),
                        ),
                        10.hGapDp,
                        Column(
                          children: [
                            Text(
                              doctorUser?.doctorName ?? '',
                              style: tsBlack,
                            ),
                            Text(
                              doctorUser?.province ??
                                  '' + doctorUser?.city ??
                                  '',
                              style: tsGrey,
                            )
                          ],
                        ),
                        10.hGapDp,
                      ],
                    ),
                    Image.memory(
                      Base64Decoder().convert(base64Str.split(',')[1]),
                      width: ScreenAdapter.screenWidthDp * 0.55,
                      height: ScreenAdapter.screenWidthDp * 0.55,
                    ),
                    15.vGapDp,
                    Text(
                      '扫描二维码，加入我的互联网诊所',
                      style: tsGrey,
                    ),
                  ],
                ),
              ),
      ),
    );
  }

  void getQrImg() async {
    var result = await LoginHttp.getQrImg(
        doctorUser.keyID, doctorUser.orgTypeID.toString(), doctorUser.phone);
    if (result.isSuccess) {
      base64Str = result.data;
      setState(() {});
    } else {
      ToastUtil.error(result.error.msg);
    }
  }
}
